<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>客户采购习惯分析</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input, select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .form-row {
            display: flex;
            gap: 15px;
        }
        .form-row .form-group {
            flex: 1;
        }
        button {
            background-color: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #0056b3;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 12px;
            text-align: left;
        }
        th {
            background-color: #f8f9fa;
            font-weight: bold;
        }
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        .pagination {
            display: flex;
            justify-content: center;
            margin-top: 20px;
            gap: 10px;
        }
        .pagination button {
            padding: 8px 12px;
        }
        .result-info {
            margin-top: 15px;
            text-align: center;
            color: #666;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>客户采购习惯分析</h1>
        
        <form id="analysisForm">
            <div class="form-row">
                <div class="form-group">
                    <label for="customerId">客户ID:</label>
                    <input type="number" id="customerId" name="customerId">
                </div>
                <div class="form-group">
                    <label for="categoryId">商品分类:</label>
                    <select id="categoryId" name="categoryId">
                        <option value="">全部</option>
                        <option value="11">叶菜</option>
                        <option value="12">根茎</option>
                        <option value="13">茄果</option>
                        <option value="14">辣椒</option>
                        <option value="15">瓜类</option>
                        <option value="16">南瓜类</option>
                        <option value="21">水果类</option>
                        <option value="22">特菜类</option>
                    </select>
                </div>
            </div>
            
            <div class="form-row">
                <div class="form-group">
                    <label for="commodityName">商品名称:</label>
                    <input type="text" id="commodityName" name="commodityName" placeholder="支持模糊搜索">
                </div>
                <div class="form-group">
                    <label for="specsName">规格名称:</label>
                    <input type="text" id="specsName" name="specsName" placeholder="支持模糊搜索">
                </div>
            </div>
            
            <div class="form-row">
                <div class="form-group">
                    <label for="startTime">开始时间:</label>
                    <input type="date" id="startTime" name="startTime">
                </div>
                <div class="form-group">
                    <label for="endTime">结束时间:</label>
                    <input type="date" id="endTime" name="endTime">
                </div>
            </div>
            
            <div class="form-row">
                <div class="form-group">
                    <label for="page">页码:</label>
                    <input type="number" id="page" name="page" value="1" min="1">
                </div>
                <div class="form-group">
                    <label for="limit">每页大小:</label>
                    <select id="limit" name="limit">
                        <option value="10">10</option>
                        <option value="20">20</option>
                        <option value="50">50</option>
                        <option value="100">100</option>
                    </select>
                </div>
            </div>
            
            <button type="submit">分析采购习惯</button>
        </form>
        
        <div id="result">
            <div class="result-info" id="resultInfo"></div>
            <table id="resultTable">
                <thead>
                    <tr>
                        <th>客户名称</th>
                        <th>商品名称</th>
                        <th>商品编码</th>
                        <th>商品分类</th>
                        <th>规格</th>
                        <th>采购次数</th>
                        <th>总数量</th>
                        <th>总重量</th>
                        <th>平均数量</th>
                        <th>平均重量</th>
                        <th>最后采购时间</th>
                    </tr>
                </thead>
                <tbody id="resultBody">
                    <!-- 结果将在这里显示 -->
                </tbody>
            </table>
            <div class="pagination" id="pagination">
                <!-- 分页控件将在这里显示 -->
            </div>
        </div>
    </div>

    <script>
        document.getElementById('analysisForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            // 收集表单数据
            const formData = new FormData(this);
            const params = {};
            for (let [key, value] of formData.entries()) {
                if (value !== '') {
                    params[key] = value;
                }
            }
            
            // 发送请求
            fetch('/outStorage/salesOrder/analyzeCustomerPurchase', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(params)
            })
            .then(response => response.json())
            .then(data => {
                if (data.code === 0) {
                    displayResults(data.data);
                } else {
                    alert('查询失败: ' + data.msg);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('查询出错，请查看控制台');
            });
        });
        
        function displayResults(data) {
            const resultBody = document.getElementById('resultBody');
            const resultInfo = document.getElementById('resultInfo');
            const pagination = document.getElementById('pagination');
            
            // 显示结果信息
            resultInfo.textContent = `共找到 ${data.total} 条记录，当前第 ${data.current} 页，共 ${data.pages} 页`;
            
            // 清空之前的结果
            resultBody.innerHTML = '';
            
            // 填充表格数据
            data.records.forEach(item => {
                const row = document.createElement('tr');
                row.innerHTML = `
                    <td>${item.customerName}</td>
                    <td>${item.commodityName}</td>
                    <td>${item.commodityCode}</td>
                    <td>${item.categoryName || '未分类'}</td>
                    <td>${item.specsName || '无规格'}</td>
                    <td>${item.purchaseCount}</td>
                    <td>${item.totalNumber}</td>
                    <td>${item.totalWeight.toFixed(2)}</td>
                    <td>${item.avgNumber ? item.avgNumber.toFixed(2) : '0.00'}</td>
                    <td>${item.avgWeight ? item.avgWeight.toFixed(2) : '0.00'}</td>
                    <td>${item.lastPurchaseTime}</td>
                `;
                resultBody.appendChild(row);
            });
            
            // 生成分页控件
            pagination.innerHTML = '';
            if (data.pages > 1) {
                for (let i = 1; i <= data.pages; i++) {
                    const pageButton = document.createElement('button');
                    pageButton.textContent = i;
                    pageButton.className = i === data.current ? 'active' : '';
                    pageButton.onclick = function() {
                        document.getElementById('page').value = i;
                        document.getElementById('analysisForm').dispatchEvent(new Event('submit'));
                    };
                    pagination.appendChild(pageButton);
                }
            }
        }
        
        // 设置默认时间范围为最近一年
        window.onload = function() {
            const today = new Date();
            const oneYearAgo = new Date();
            oneYearAgo.setFullYear(today.getFullYear() - 1);
            
            document.getElementById('startTime').value = oneYearAgo.toISOString().split('T')[0];
            document.getElementById('endTime').value = today.toISOString().split('T')[0];
        };
    </script>
</body>
</html>